﻿using System;
using System.Collections.Generic;
using System.Text;

namespace OpcSelfHost.printer
{
    class BrPrinterData
    {
        static BrPrinterData instance = null;
        private static readonly object padlock = new object();

        List<byte> listData;
        private BrPrinterData()
        {

            listData = new List<byte>();
        }

        public static BrPrinterData Init
        {
            get
            {
                if (instance == null)
                {
                    lock (padlock)
                    {
                        if (instance == null)
                        {
                            instance = new BrPrinterData();
                        }
                    }
                }
                return instance;
            }
        }

        /// <summary>
        /// 初始化，清空数组
        /// </summary>
        public void init() {

            if (listData == null) {

                listData = new List<byte>();
            
            }


            listData.Clear();
        
        
        }

        /// <summary>
        /// 添加byte数组到list
        /// </summary>
        /// <param name="data">byte数组</param>
        public void addDataByte(byte[] data) {

            if (listData != null) { 
            
            
                foreach(byte b in data){
                
                listData.Add(b);

                }
            
            }
        
        
        }


        public void addDataByte(String str) {

        byte[] data =    Encoding.GetEncoding("GBK").GetBytes(str);

        addDataByte(data);
        
        }
        public void addDataByte(byte b) {

            if (listData != null)
            {



                    listData.Add(b);


            }
        
        }

        /// <summary>
        /// 读取存储到list数组
        /// </summary>
        /// <returns></returns>
        public byte[] getDataByte() {


            if (listData == null) {

                return new byte[] { 0x0a};
            }

            int dataLen = listData.Count;

            byte[] retData = new byte[dataLen];
            int iIndex = 0;
            foreach (byte b in listData) {

                retData[iIndex++] = b;
            
            }


            return retData;


        }
        public byte[] getEncoding(string str)
        {

            return Encoding.GetEncoding("GBK").GetBytes(str);
        }
        public byte[] getEncodingAscii(string str)
        {

            return Encoding.ASCII.GetBytes(str);
        }

     
  










    }
}
